#include "gui.h"

GLuint gui::tex;
GLuint* gui::numarr;

gui::gui()
{

}

gui::gui(player & theplayer)
{
    p1 = &theplayer;
    timetonextround = TIME_BETWEEN_WAVES;
}

gui::~gui()
{
    delete [] numarr;
}

void gui::draw(ptrContainer & dcontainer)
{
    //height:64px

    //Display the background
    glPushMatrix();
    glTranslatef(0,704,0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glBegin(GL_QUADS);
    
    glTexCoord2i(0,0); //Texture coord
    glVertex3f(0,0,0); //Point coord

    glTexCoord2i(1,0);
    glVertex3f(1024,0,0);
    
    glTexCoord2i(1,1);
    glVertex3f(1024,64,0);

    glTexCoord2i(0,1);
    glVertex3f(0,64,0);
    glEnd();
    glPopMatrix();
    glLoadIdentity();
    //Done displaying background

    //Display currently building tower
    glPushMatrix();
    glTranslatef(80,704,0);
    glBindTexture(GL_TEXTURE_2D, dcontainer.towerTex[dcontainer.curTower]);
    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex3f(0,0,0);
    glTexCoord2i(1,0);
    glVertex3f(64,0,0);
    glTexCoord2i(1,1);
    glVertex3f(64,64,0);
    glTexCoord2i(0,1);
    glVertex3f(0,64,0);
    glEnd();
    glPopMatrix();

    //Display Current Tower Name
    drawstr(dcontainer.towerNames[dcontainer.curTower],80+64,731, 16, 32);
    //Display Current Tower Cots
    stringstream str;
    str << "Cost $" << dcontainer.towerCost[dcontainer.curTower];
    
    drawstr(str.str(), 80+64, 700, 16, 32);


    //Display health bar
    glPushMatrix();
    glTranslatef(5,736,0);
    glDisable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glColor4f(1,0,0,1);
    glVertex3f(0,0,0);
    glVertex3f(p1->maxhp*HPBARSCALAR,0,0);
    glVertex3f(p1->maxhp*HPBARSCALAR,27,0);
    glVertex3f(0,27,0);
    glEnd();

    //Green bar
    glBegin(GL_QUADS);
    glColor4f(0,1,0,1);
    glVertex3f(0,0,0);
    glVertex3f(p1->curhp*HPBARSCALAR,0,0);
    glVertex3f(p1->curhp*HPBARSCALAR,27,0);
    glVertex3f(0,27,0);
    glEnd();



    glEnable(GL_TEXTURE_2D);
    glPopMatrix();
    glLoadIdentity();
    glColor4f(1,1,1,1);


    //Print cash
    drawnum(p1->cash,1019,731, 16, 32, 1, 1, 1, 1);

    //Print time
    drawnum((int)timetonextround,900,731,16,32,1,1,1,1);

    
    
}

void gui::drawdownbar(){
    //Display the background
    glPushMatrix();
    glTranslatef(0,704,0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glBegin(GL_QUADS);
    
    glTexCoord2i(0,0); //Texture coord
    glVertex3f(0,0,0); //Point coord

    glTexCoord2i(1,0);
    glVertex3f(1024,0,0);
    
    glTexCoord2i(1,1);
    glVertex3f(1024,64,0);

    glTexCoord2i(0,1);
    glVertex3f(0,64,0);
    glEnd();
    glPopMatrix();
    glLoadIdentity();
    //Done displaying background
}

void gui::drawnum(int num, int x, int y, int charw, int charh, float r, float g, float b, float a)
{
    //X and Y are top right corner
    
    stringstream s;
    s << num;
    string str = s.str();
    for (int i = str.size()-1; i >= 0; i--)
    {
        char curchar = str[i];
        int curint = curchar;
        x -= charw;
        glPushMatrix();
        glTranslatef(x,y,0);
        glColor4f(r,g,b,a);

        glBindTexture(GL_TEXTURE_2D, numarr[curint]);
        glBegin(GL_QUADS);
	
        glTexCoord2i(0,0); //Texture coord
        glVertex3f(0,0,0); //Point coord
	
        glTexCoord2i(1,0);
        glVertex3f(charw,0,0);
	
        glTexCoord2i(1,1);
        glVertex3f(charw,charh,0);
	
        glTexCoord2i(0,1);
        glVertex3f(0,charh,0);
        glEnd();
        glPopMatrix();
        glLoadIdentity();
        glColor4f(1,1,1,1);
	
    }


}

void gui::drawstr(string num, int x, int y, int charw, int charh, float r, float g, float b, float a)
{
    //X and Y are top left corner
    
    string str = num;
    x -= charw;
    for (unsigned int i = 0; i < str.size(); i++)
    {
        char curchar = str[i];
        int curint = curchar;
        x += charw;
        glPushMatrix();
        glTranslatef(x,y,0);
        glColor4f(r,g,b,a);

        glBindTexture(GL_TEXTURE_2D, numarr[curint]);
        glBegin(GL_QUADS);
	
        glTexCoord2i(0,0); //Texture coord
        glVertex3f(0,0,0); //Point coord
	
        glTexCoord2i(1,0);
        glVertex3f(charw,0,0);
	
        glTexCoord2i(1,1);
        glVertex3f(charw,charh,0);
	
        glTexCoord2i(0,1);
        glVertex3f(0,charh,0);
        glEnd();
        glPopMatrix();
        glLoadIdentity();
        glColor4f(1,1,1,1);
	
    }


}

void gui::drawstrleft(string num, int x, int y, int charw, int charh, float r, float g, float b, float a)
{
    //X and Y are top right corner
    
    string str = num;
    //x -= charw;
    for (unsigned int i = str.size()-1; i >= 0; i--)
    {
        char curchar = str[i];
        int curint = curchar;
        x -= charw;
        glPushMatrix();
        glTranslatef(x,y,0);
        glColor4f(r,g,b,a);

        glBindTexture(GL_TEXTURE_2D, numarr[curint]);
        glBegin(GL_QUADS);
	
        glTexCoord2i(0,0); //Texture coord
        glVertex3f(0,0,0); //Point coord
	
        glTexCoord2i(1,0);
        glVertex3f(charw,0,0);
	
        glTexCoord2i(1,1);
        glVertex3f(charw,charh,0);
	
        glTexCoord2i(0,1);
        glVertex3f(0,charh,0);
        glEnd();
        glPopMatrix();
        glLoadIdentity();
        glColor4f(1,1,1,1);
	
    }


}

bool gui::updateTime(double prog)
{
    timetonextround -= prog;
    if (timetonextround <= 0)
        return true;
    return false;
}
